<?php

/*
 * - Okek CMS
 * - create by Jg and Tito
 * - Okek Projects 2012
 * - CMS developed for free use
 * - More info http://habbo-dev.com
 */

class Lang {
    
    public $params = array();
    public $section = array();
    
    public function Init($lang) {
        
        global $io, $core;
        
        $folder = LANGS . $lang;
        
        if(!is_dir($folder)) {
            $folder = LANGS . 'en';
        }
        
        $files = $io->GetDir($folder);
        
        foreach($files as $file)
        {
            if($file == 'Codes.json' OR !Contains($file, 'json'))
                    continue;

            $this->SetFileLang($folder . DS . $file);
        }
    }
    
    function SetFileLang($file) {
        global $core;
        
        if(!file_exists($file)) {
            return false;
        }
        
        $data = $core->LoadJSON($file);
        $this->params = array_merge($data, $this->params);
    }
    
    function SetParams($data, $section = '') {
        if(empty($section))
                $section = $this->section;

        if(is_array($section))
        {
            foreach($section as $sec)
            {
                if(empty($this->params[$sec]))
                        continue;

                foreach($this->params[$sec] as $param => $value)
                {
                    $value = utf8_decode($value);
                    $value = ShortCuts($value);

                    $data = str_ireplace('%' . $param . '%', $value, $data);
                }
            }
        }
        else
        {
            if(empty($this->params[$section]))
                return $data;

            foreach($this->params[$section] as $param => $value)
                $data = str_ireplace('%' . $param . '%', utf8_decode($value), $data);
        }

        return $data;
    }
    
    function PrepareLive($section = '')
	{       
		global $config, $core, $tpl;

		if(empty($section))
			$section = $tpl->lang_selection;

		if(is_array($section) AND count($section) == 1)
			$section = $section[0];

		$result = array();
		$params = $this->params;
                
		foreach($params as $lang => $value)
		{
                        
			if(is_array($section))
			{
				foreach($section as $sec)
				{
					$lvalue = $params[$lang][$sec];
                                        
                                        

					if(empty($lvalue))
						continue;

					if(CHARSET !== 'UTF-8')
						$lvalue = $core->UTF8Decode($lvalue);

					$lvalue = ShortCuts($lvalue);
					$result[$lang][$sec] = $lvalue;
				}
			}
			else
			{
				$lvalue = $params[$lang][$section];

				if(empty($lvalue))
					return;

				if(CHARSET !== 'UTF-8')
					$lvalue = $core->UTF8Decode($lvalue);

				$lvalue = ShortCuts($lvalue);
				$result[$lang] = $lvalue;
			}
		}

		return $result;
	}
}
?>
